package com.cloud.client.cloudclient;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import com.springcoud.domain.springcouddomain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableCircuitBreaker
public class CloudclientApplication {

	@Autowired
	private EurekaClient eurekaClient;

	@Autowired
	RestTemplateBuilder restTemplateBuilder;

	@Bean
	public RestTemplate getRestTemplate(){
		return restTemplateBuilder.build();
	}

	@Autowired
	private RestTemplate restTemplate;


	public static void main(String[] args) {
		SpringApplication.run(CloudclientApplication.class, args);
	}


	@Value("${server.port}")
	String port;
	@RequestMapping("/")
	public String home() {
		return "hello world from port " + port;
	}
	@RequestMapping(value = "/getServer",method = RequestMethod.GET)
	private String getCloudServer(){
		InstanceInfo nextServerFromEureka = eurekaClient.getNextServerFromEureka("client2-service", false);
		return restTemplate.getForObject(nextServerFromEureka.getHomePageUrl()+"/",String.class);
	}

	@RequestMapping(value = "/hello",method = RequestMethod.GET)
	private String getFeiginServer(){
		System.out.println("访问来1了......");
		return "hello1";
	}

	@RequestMapping(value = "/user",method = RequestMethod.POST)
	private User getUser(@RequestBody User user){
		System.out.println("访问来2了......");
		return user;
	}


	@RequestMapping(value = "/name",method = RequestMethod.GET)
	private String getUserName(@RequestParam  String name){
		System.out.println("访问来3了......");
		return name;
	}
	@RequestMapping(value = "/getId/{salesmanId}",method = RequestMethod.GET)
	String searchLoaning(@PathVariable("salesmanId") String salesmanId,
						 @RequestParam(value = "loanParam",required = false) Map<String,Object> loanParam){
		return salesmanId;

	}

	@Bean
	public ServletRegistrationBean getServlet(){
		HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
		ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
		registrationBean.setLoadOnStartup(1);
		registrationBean.addUrlMappings("/hystrix.stream");
		registrationBean.setName("HystrixMetricsStreamServlet");
		return registrationBean;
	}


}